package com.wangnian.wangcai.scheduled;

import com.wangnian.wangcai.config.Config;
import com.wangnian.wangcai.dao.DayInfoDao;
import com.wangnian.wangcai.dao.NameInfoDao;
import com.wangnian.wangcai.service.AutoService;
import com.wangnian.wangcai.service.BusinessService;
import com.wangnian.wangcai.service.SyncService;
import com.wangnian.wangcai.util.ChatbotSend;
import com.wangnian.wangcai.util.DateUtil;
import com.wangnian.wangcai.util.ThsCount;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import static com.wangnian.wangcai.util.DateUtil.getNowDate;

@Component
public class RunScheduled {

    final SyncService syncService;
    final AutoService autoService;
    final BusinessService businessService;
    final NameInfoDao nameInfoDao;
    final InitService initService;
    final DayInfoDao dayInfoDao;

    private final static Logger logger = LoggerFactory.getLogger(RunScheduled.class);

    private static boolean tasking = true;

    public RunScheduled(SyncService syncService, AutoService autoService, BusinessService businessService, NameInfoDao nameInfoDao, InitService initService, DayInfoDao dayInfoDao) {
        this.syncService = syncService;
        this.autoService = autoService;
        this.businessService = businessService;
        this.nameInfoDao = nameInfoDao;
        this.initService = initService;
        this.dayInfoDao = dayInfoDao;
    }

    /**
     * 自动还款任务
     */
    @Scheduled(cron = "0 55 14 * *  MON-FRI")
    public void autoRepayment() {
        // ApiChannel.apiService.autoRepayment();
    }


    /**
     * 每天重新获取行情
     */
    @Scheduled(cron = "0 10 9 * *  MON-FRI")
    public void runDay() {
        try {
            //删除所有历史数据
            nameInfoDao.deleteAll();
            dayInfoDao.deleteAll();

            //初始化name 和 chigu
            initService.init();
            Config.allowRefresh();

            //根据 name 更新所有的行情数据
            DateUtil.NowDate nowDate = getNowDate();
            int hours = nowDate.getHours();
            String endDateStr;
            if (hours < 15) {
                //取昨天的行情
                endDateStr = DateUtil.subDay(1, DateUtil.YYYYMMDD);
            } else {
                //取今天的行情
                endDateStr = DateUtil.subDay(0, DateUtil.YYYYMMDD);
            }
            syncService.runDay(DateUtil.subDay(40, DateUtil.YYYYMMDD), endDateStr);
            //测试一下，如果有问题会警告
            syncService.syncShorHold(-2);
            //可以运行了（中途启动用）
            RunScheduled.tasking = false;
            //这个用来统计满足
            ThsCount.thsCount = 0;
            //初始化一下
            Config.allowRefresh();
            //默认开通自动定投
            Config.configMap.put(Config.autoDT, 1);
            //清掉昨天的数据
            Config.dayStr.clear();
        } catch (Exception ex) {
            logger.error("", ex);
            ChatbotSend.send("获取行情数据出错");
        }
    }

    /**
     * 实时监控做T
     */
    @Scheduled(cron = "0/10 * 9,10,11,13,14 ? * MON-FRI")
    public void task1() {
        if (RunScheduled.tasking) {
            return;
        }
        DateUtil.NowDate nowDate = getNowDate();
        int hours = nowDate.getHours();
        int minutes = nowDate.getMinutes();
        if ((hours == 9 && minutes < 29) || (hours == 11 && minutes > 30) || (hours == 14 && minutes > 55)) {
            return;
        }

        try {
            syncService.syncShorHold(minutes);
        } catch (Exception e) {
            logger.error("监控异常", e);
            ChatbotSend.send("监控报错");
        }
    }

}
